{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T01:39:37.412612Z",
     "start_time": "2025-09-02T01:39:35.505124Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import  ChatOpenAI\n",
    "from langchain_core.prompts import  ChatPromptTemplate\n",
    "from langchain_core.output_parsers import StrOutputParser,JsonOutputParser\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "load_dotenv('../.env')\n",
    "# 结果解释器\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    api_key=os.getenv(\"DEEPSEEK_API_KEY\"),  # 替换为你的 API Key\n",
    "    base_url=os.getenv(\"DEEPSEEK_BASE_URL\"),  # DeepSeek API 地址\n",
    "    model=\"deepseek-chat\",\n",
    "    temperature=0.5,\n",
    ")\n",
    "\n",
    "\n",
    "\n"
   ],
   "id": "adde26d2de4478da",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T01:39:40.023438Z",
     "start_time": "2025-09-02T01:39:40.010043Z"
    }
   },
   "cell_type": "code",
   "source": [
    "output_parsers  = StrOutputParser()\n",
    "# output_parsers  = JsonOutputParser()\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\",\"你是和蔼的小助手\"),\n",
    "    (\"user\",\"{input}\")\n",
    "])"
   ],
   "id": "2abaeaf055d3c375",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-02T01:39:47.564898Z",
     "start_time": "2025-09-02T01:39:41.279025Z"
    }
   },
   "cell_type": "code",
   "source": [
    "chain = prompt | llm | output_parsers\n",
    "response = chain.invoke({\"input\":\"中国有多少省份？多少地级市 用一问一答的方式 question answer\" })\n",
    "print(response)"
   ],
   "id": "269d1f65006fe6c0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "question: 中国有多少省份？  \n",
      "answer: 中国共有34个省级行政区，其中包括23个省、5个自治区、4个直辖市和2个特别行政区。\n",
      "\n",
      "question: 中国有多少地级市？  \n",
      "answer: 截至2023年，中国共有293个地级市。\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "本代码仅为测试",
   "id": "1cae9667d7f400d5"
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
